package com.geeklan.smart.ui.dialog;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.geeklan.smart.R;
import com.geeklan.smart.databinding.DialogContentWeekBinding;
import com.geeklan.smart.utils.DensityUtils;

import java.util.HashMap;
import java.util.Map;

public class WeekBottomSheetDialog extends BaseBottomSheetDialog<DialogContentWeekBinding> implements View.OnClickListener {
    private Map<Integer, Boolean> map = new HashMap<>();
    private SelectWeekListener selectWeekListener;

    public void setSelectWeekListener(SelectWeekListener selectWeekListener) {
        this.selectWeekListener = selectWeekListener;
    }

    public WeekBottomSheetDialog(Context context) {
        super(context);
    }

    @Override
    protected DialogContentWeekBinding createContentViewBinding(LayoutInflater inflater) {
        return DialogContentWeekBinding.inflate(inflater);
    }

    @Override
    protected void setupDialog() {
        super.setupDialog();
        setDialogTitle(mContext.getString(R.string.settings_feed_pan_loop));
//        setContentViewHeight(DensityUtils.dpToPx(mContext, com.smart.base.R.dimen.dp_340));
        setConfirmButtonVisible(true);
        setNonFullScreen();
        initView();
    }

    private void initView() {
        setConfirmButtonText(mContext.getString(R.string.save));
        setConfirmButtonClickListener(v -> {
            boolean isEmpty = true;
            if(map.isEmpty()){
                Toast.makeText(mContext,"请选择重复周期",Toast.LENGTH_SHORT).show();
                return;
            }

            for (Map.Entry<Integer, Boolean> entry : map.entrySet()) {
                if (entry.getValue()) {
                    isEmpty = false;
                }
            }
            if(isEmpty){
                Toast.makeText(mContext,"请选择重复周期",Toast.LENGTH_SHORT).show();
                return;
            }

            StringBuilder sb = new StringBuilder();
            if(map.containsKey("0") && map.get(0)){
                sb.append("0");
            }else {
                for (Map.Entry<Integer, Boolean> entry : map.entrySet()) {
                    if (entry.getValue()) {
                        sb.append(entry.getKey()).append(",");
                    }
                }
                sb.deleteCharAt(sb.length() - 1);
            }

            selectWeekListener.weeks(sb.toString());
            dismiss();
        });

        mBinding.llItem0.setOnClickListener(this);
        mBinding.llItem1.setOnClickListener(this);
        mBinding.llItem2.setOnClickListener(this);
        mBinding.llItem3.setOnClickListener(this);
        mBinding.llItem4.setOnClickListener(this);
        mBinding.llItem5.setOnClickListener(this);
        mBinding.llItem6.setOnClickListener(this);
        mBinding.llItem7.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v instanceof LinearLayout) {
            LinearLayout linearLayout = (LinearLayout) v;
            ImageView imageView = (ImageView) linearLayout.getChildAt(1);

            if (imageView.getTag().equals("0")) {
                imageView.setTag("1");
                imageView.setImageResource(R.drawable.ic_checkbox_select);
                map.put(Integer.parseInt(linearLayout.getTag().toString()), true);
                if (v.getId() == mBinding.llItem0.getId()) {
                    selectAll(true);
                }else {
                    boolean isAll = true;
                    for (int i = 1; i < mBinding.getRoot().getChildCount(); i++) {
                        LinearLayout childLinearLayout = (LinearLayout) mBinding.getRoot().getChildAt(i);
                        ImageView iv = (ImageView) childLinearLayout.getChildAt(1);
                        if(iv.getTag().equals("0")){
                            isAll = false;
                        }
                    }
                    if(isAll){
                        map.put(Integer.parseInt("0"), true);
                        mBinding.cbItem1.setImageResource(R.drawable.ic_checkbox_select);
                    }else {
                        map.put(Integer.parseInt("0"), false);
                        mBinding.cbItem1.setImageResource(R.drawable.ic_checkbox_def);
                    }
                }
            } else {
                imageView.setTag("0");
                imageView.setImageResource(R.drawable.ic_checkbox_def);
                map.put(Integer.parseInt(linearLayout.getTag().toString()), false);
                if (v.getId() == mBinding.llItem0.getId()) {
                    selectAll(false);
                }else {
                    boolean isAll = true;
                    for (int i = 1; i < mBinding.getRoot().getChildCount(); i++) {
                        LinearLayout childLinearLayout = (LinearLayout) mBinding.getRoot().getChildAt(i);
                        ImageView iv = (ImageView) childLinearLayout.getChildAt(1);
                        if(iv.getTag().equals("0")){
                            isAll = false;
                        }
                    }
                    if(isAll){
                        map.put(Integer.parseInt("0"), true);
                        mBinding.cbItem1.setImageResource(R.drawable.ic_checkbox_select);
                    }else {
                        map.put(Integer.parseInt("0"), false);
                        mBinding.cbItem1.setImageResource(R.drawable.ic_checkbox_def);
                    }
                }
            }
        }
    }

    public void selectAll(boolean isSelect) {
        for (int i = 1; i < mBinding.getRoot().getChildCount(); i++) {
            LinearLayout childLinearLayout = (LinearLayout) mBinding.getRoot().getChildAt(i);
            ImageView imageView = (ImageView) childLinearLayout.getChildAt(1);

            if (isSelect) {
                imageView.setTag("1");
                imageView.setImageResource(R.drawable.ic_checkbox_select);
                map.put(Integer.parseInt(childLinearLayout.getTag().toString()), true);
            } else {
                imageView.setTag("0");
                imageView.setImageResource(R.drawable.ic_checkbox_def);
                map.put(Integer.parseInt(childLinearLayout.getTag().toString()), false);
            }
        }
    }

    public interface SelectWeekListener {
        void weeks(String weeks);
    }
}
